package com.church.action;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.church.base.common.util.ExtData;
import com.church.db.Curriculum;
import com.church.db.CurriculumList;
import com.church.services.CurriculumListServices;
import com.church.services.CurriculumServices;
import com.opensymphony.xwork2.ActionContext;

public class CurriculumListAction extends ChurchActionSupport {
	private CurriculumListServices curriculumListServices;
	private CurriculumServices curriculumServices;
	private boolean success;
	public String getCurriculumListByUserName() {

		ExtData<CurriculumList> extData = new ExtData<CurriculumList>();
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		String userName = (String) session.get("USER_NAME");
		if (userName != null) {
			List<CurriculumList> list = curriculumListServices
					.getCurriculumListByUserName(userName);
			extData.setSuccess(true);
			extData.setRoot(list);
		}
		return SUCCESS;
	}

	public String getClassList() {
		ExtData<Curriculum> extData = new ExtData<Curriculum>();
		List<Curriculum> list = curriculumServices.getAll();
		extData.setSuccess(true);
		extData.setRoot(list);
		return SUCCESS;
	}
	
	public String addCurriculumList() {
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		String userName = (String) session.get("USER_NAME");
		if (userName != null) {
			String json = this.getJsonObject();
			JSONArray jsonArray = JSONArray.fromObject(json);
			for (int i = 0; i < jsonArray.size(); i++) {
				int curriculumId = jsonArray.getInt(i);
				CurriculumList cl = new CurriculumList();
				cl.setCurriculumId(curriculumId);
				cl.setUserName(userName);
				cl.setState("0");
				curriculumListServices.addCurriculumList(cl);
			}
		}
		success=true;
		return SUCCESS;
	}
	public String removeCurriculum()
	{
		String json = this.getJsonObject();
		JSONObject jb=JSONObject.fromObject(json);
		int curriculumId = jb.getInt("curriculumId");
		curriculumListServices.removeCurriculumListById(curriculumId);
		success=true;
		return SUCCESS;
	}
	public CurriculumListServices getCurriculumListServices() {
		return curriculumListServices;
	}

	public void setCurriculumListServices(
			CurriculumListServices curriculumListServices) {
		this.curriculumListServices = curriculumListServices;
	}

	public CurriculumServices getCurriculumServices() {
		return curriculumServices;
	}

	public void setCurriculumServices(CurriculumServices curriculumServices) {
		this.curriculumServices = curriculumServices;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

}
